from rest_framework import serializers

from api.models import Basket, BasketLine, Porduct
from api.models.user import ShipAddress
from api.models.advertisement import AdvertiseMent
from api.routes.porduct.serializer import PorductDataSerializer
from api.utils.enums import BasketType, PriceType

from api.utils.serializers import PaginationSerializer, PaginationResultSerializer


class AdminProductAdvertisementQeurySerializer(serializers.ModelSerializer):
    product = serializers.CharField(help_text="商品ID", max_length=256, required=True)

    class Meta:
        model = AdvertiseMent
        fields = ("file", "type", "product")
        extra_kwargs = {
            "file": {"required": True},
            "type": {"required": False},
        }

    def validate_product(self, product):
        try:
            product = Porduct.objects.get(product_id=product)
        except Exception as e:
            raise "未找到产品"

        return product


class AdminProductAdvertisementIDSerializer(serializers.ModelSerializer):
    class Meta:
        model = AdvertiseMent
        fields = ("id",)


class AdminProductAdvertisementQuerySerializer(serializers.ModelSerializer):
    class Meta:
        model = AdvertiseMent
        fields = ("type",)


class AdminProductAdvertisementBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = AdvertiseMent
        fields = ["file", "type", "products_id"]


class AdvertisementBodySerializer(PaginationResultSerializer):
    data = AdminProductAdvertisementBodySerializer(many=True)
